home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Utilities ƒ / MPW Tools ƒ / MakeMake / Source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-12-10  |  9.2 KB  |  431 lines  |  [TEXT/MPS ]

  1. # include "defs.h"
  2.  
  3. char *Usage =
  4. "Usage: Makemake [-p] [name=value…] [-i dir]… { -t target sources… }…\n";
  5.  
  6. char *progname = "Makemake";
  7.  
  8. main (argc, argv)
  9. int argc;
  10. char *argv[];
  11.     {
  12.     int i;                    /* Loop counter                        */
  13.     int t = 0;                /* Counts targets ("-t" arguments)    */
  14.     int oops = 0;            /* Counts errors in arguments        */
  15.     char *sfx;                /* Pointer to filename suffix        */
  16.     extern char *obj ();
  17.     extern char *getmacro ();
  18.     extern char *strchr ();
  19.     extern char *strrchr ();
  20.     
  21.     
  22.     progname = argv[0];
  23.     
  24.     /*
  25.      * For each command line argument
  26.      */
  27.     for (i = 1; i < argc; i++)
  28.         {
  29.         /*
  30.          * Flags begin with a dash
  31.          */
  32.         if (argv[i][0] == '-') switch (argv[i][1])
  33.             {
  34.             case 'I':        /* Include directory    */
  35.             case 'i':        /* Include directory    */
  36.                 i++;
  37.                 if (i < argc)
  38.                     {
  39.                     add_dir (argv[i], adirlist);
  40.                     add_dir (argv[i], cdirlist);
  41.                     add_dir (argv[i], pdirlist);
  42.                     }
  43.                 break;
  44.             case 'T':        /* Target                */
  45.             case 't':        /* Target                */
  46.                 t++;        /* Count it                */
  47.                 i++;
  48.                 if (t == TARGETMAX)
  49.                     goof ("Too many targets (max %d).\n", TARGETMAX);
  50.                 else
  51.                     targets[t-1] = argv[i];
  52.                 break;
  53.             case 'P':
  54.             case 'p':
  55.                 verbose = TRUE;
  56.                 progress ("Makemake 1.1, by Rick Holzgrafe\n");
  57.                 break;
  58.             default:
  59.                 goof ("Unrecognized option \"%s\"\n", argv[i]);
  60.                 oops++;
  61.                 break;
  62.             } /* End if-switch */
  63.             
  64.         else if (strchr (argv[i], '=') != NULL)    /* It's a macro        */
  65.             setmacro (argv[i]);                    /* If it has an '='    */
  66.         
  67.         else    /* A source file name    */
  68.             {
  69.             if (t == 0)
  70.                 {
  71.                 goof ("A target must be listed before its sources or libraries.\n");
  72.                 oops++;
  73.                 }
  74.             else
  75.                 {
  76.                 /*
  77.                  * Check the filename suffix.
  78.                  * No suffix means a simple dependency;
  79.                  * .o is a library (and therefore a dependency)
  80.                  *        but not a source;
  81.                  * any other suffix is a source which produces an object,
  82.                  *        which in turn is a dependency.
  83.                  */
  84.                 sfx = strrchr (argv[i], '.');
  85.                 if (strcmp (sfx, ".c") == 0
  86.                  || strcmp (sfx, ".p") == 0
  87.                  || strcmp (sfx, ".a") == 0)
  88.                     {
  89.                     add_source (argv[i]);
  90.                     add_object (t - 1, obj (argv[i]));
  91.                     }
  92.                 else if (strcmp (sfx, ".o") == 0)
  93.                     add_lib (t - 1, argv[i]);
  94.                 else if (strcmp (sfx, ".r") == 0)
  95.                     {
  96.                     add_res (t - 1, argv[i]);
  97.                     add_dot_r (argv[i]);
  98.                     }
  99.                 else
  100.                     add_depend (t - 1, argv[i]);
  101.                 }
  102.             }
  103.         } /* End for loop */
  104.         
  105.     if (t == 0)        /* No targets given */
  106.         oops++;
  107.     
  108.     /*
  109.      * If errors were found in the command line, complain
  110.      * usefully and exit.
  111.      */
  112.     if (oops > 0)
  113.         {
  114.         usage ();
  115.         exit (EXIT_OPT);
  116.         }
  117.     
  118.     /*
  119.      * Add default include dirs to list.
  120.      */
  121.     add_def_dirs ();
  122.     
  123.     /*
  124.      * Set up defaults macros.  Since they are defaults,
  125.      * we set them up only if they are not already set
  126.      * (by the command line).
  127.      */
  128.     if (getmacro ("DUP") == NULL)
  129.         smacro ("DUP", "Duplicate");
  130.     if (getmacro ("DUPOPTS") == NULL)
  131.         smacro ("DUPOPTS", "-y");
  132.     if (getmacro ("COUNT") == NULL)
  133.         smacro ("COUNT", "Count");
  134.     if (getmacro ("COUNTOPTS") == NULL)
  135.         smacro ("COUNTOPTS", "");
  136.     if (getmacro ("CTAGS") == NULL)
  137.         smacro ("CTAGS", "Ctags");
  138.     if (getmacro ("CTAGSOPTS") == NULL)
  139.         smacro ("CTAGSOPTS", "-p -update");
  140.     if (getmacro ("DELETE") == NULL)
  141.         smacro ("DELETE", "Delete");
  142.     if (getmacro ("DELETEOPTS") == NULL)
  143.         smacro ("DELETEOPTS", "-y");
  144.     if (getmacro ("FILES") == NULL)
  145.         smacro ("FILES", "Files");
  146.     if (getmacro ("FILESOPTS") == NULL)
  147.         smacro ("FILESOPTS", "-l");
  148.     if (getmacro ("LINK") == NULL)
  149.         smacro ("LINK", "Link");
  150.     if (getmacro ("LINKOPTS") == NULL)
  151.         smacro ("LINKOPTS", "-t 'MPST' -c 'MPS '");
  152.     if (getmacro ("LINKTEMP") == NULL)
  153.         smacro ("LINKTEMP", "link.out");
  154.     if (getmacro ("MAKEFILE") == NULL)
  155.         smacro ("MAKEFILE", "Makefile");
  156.     if (getmacro ("POST") == NULL)
  157.         smacro ("POST", "echo");
  158.     if (getmacro ("POSTOPTS") == NULL)
  159.         smacro ("POSTOPTS", "'Build of'");
  160.     if (getmacro ("POSTARGS") == NULL)
  161.         smacro ("POSTARGS", "'is complete.'");
  162.     if (getmacro ("PRINT") == NULL)
  163.         smacro ("PRINT", "Print");
  164.     if (getmacro ("PRINTOPTS") == NULL)
  165.         smacro ("PRINTOPTS", "");
  166.     if (getmacro ("RENAME") == NULL)
  167.         smacro ("RENAME", "Rename");
  168.     if (getmacro ("RENAMEOPTS") == NULL)
  169.         smacro ("RENAMEOPTS", "-y");
  170.     if (getmacro ("REZ") == NULL)
  171.         smacro ("REZ", "Rez");
  172.     if (getmacro ("REZOPTS") == NULL)
  173.         smacro ("REZOPTS", "-o");
  174.     
  175.     /*
  176.      * All set -- build that makefile!
  177.      */
  178.     makemake (argc, argv);
  179.     exit (EXIT_OK);
  180.     }
  181.  
  182. /*
  183.  * Add a filename to the list of source files,
  184.  * but only if it's not already there.
  185.  */
  186. add_source (file)
  187. char *file;
  188.     {
  189.     int i;
  190.     
  191.     for (i = 0; i < SOURCEMAX && sources[i] != NULL; i++)
  192.         if (strcmp (file, sources[i]) == 0)
  193.             return;        /* Got it already */
  194.     
  195.     if (i == SOURCEMAX)
  196.         goof ("Too many source files (max %d).\n", SOURCEMAX);
  197.     else
  198.         sources[i] = file;
  199.     }
  200.  
  201. /*
  202.  * Add a filename to the list of local C include files,
  203.  * but only if it's not already there.
  204.  * These must be copied to permanent storage.
  205.  */
  206. add_dot_h (file)
  207. char *file;
  208.     {
  209.     int i;
  210.     extern char *malloc ();
  211.     
  212.     for (i = 0; i < DOTHMAX && dot_h[i] != NULL; i++)
  213.         if (strcmp (file, dot_h[i]) == 0)
  214.             return;        /* Got it already */
  215.         
  216.     if (i == DOTHMAX)
  217.         goof ("Too many local include files (max %d).\n",
  218.             DOTHMAX);
  219.     else
  220.         {
  221.         dot_h[i] = malloc ((unsigned) strlen (file) + 1);
  222.         if (dot_h[i] == NULL)
  223.             nomemory ();
  224.         strcpy (dot_h[i], file);
  225.         }
  226.     }
  227.  
  228. /*
  229.  * Add a filename to the list of local Rez files,
  230.  * but only if it's not already there.
  231.  */
  232. add_dot_r (file)
  233. char *file;
  234.     {
  235.     int i;
  236.     
  237.     for (i = 0; i < DOTHMAX && dot_r[i] != NULL; i++)
  238.         if (strcmp (file, dot_r[i]) == 0)
  239.             return;        /* Got it already */
  240.         
  241.     if (i == DOTHMAX)
  242.         goof ("Too many local Rez files (max %d).\n",
  243.             DOTHMAX);
  244.     else
  245.         dot_r[i] = file;
  246.     }
  247.  
  248. /*
  249.  * Add a directory to a list of include directories.
  250.  */
  251. add_dir (dir, list)
  252. char *dir;
  253. char *list[];
  254.     {
  255.     int i;
  256.     
  257.     for (i = 0; i < DIRMAX && list[i] != NULL; i++)
  258.         ;
  259.     if (i == DIRMAX)
  260.         goof ("Too many include directories (max %d).\n",
  261.             DIRMAX);
  262.     else
  263.         list[i] = dir;
  264.     }
  265.  
  266. /*
  267.  * Add a filename to the list of object files for the given target.
  268.  * These must be copied to permanent storage.
  269.  */
  270. add_object (t, file)
  271. int t;        /* Index of the target    */
  272. char *file;    /* Object file name        */
  273.     {
  274.     int i;
  275.     extern char *malloc ();
  276.     
  277.     for (i = 0; i < SOURCEMAX && objects[t][i] != NULL; i++)
  278.         ;
  279.     if (i == SOURCEMAX)
  280.         goof ("Too many object files for \"%s\" (max %d).\n",
  281.             targets[t], SOURCEMAX);
  282.     else
  283.         {
  284.         objects[t][i] = malloc ((unsigned) strlen (file) + 1);
  285.         if (objects[t][i] == NULL)
  286.             nomemory ();
  287.         strcpy (objects[t][i], file);
  288.         }
  289.     }
  290.  
  291. /*
  292.  * Add a library to the list of library files for the given target.
  293.  * These must be copied to permanent storage.
  294.  */
  295. add_lib (t, file)
  296. int t;        /* Index of the target    */
  297. char *file;    /* Library file name    */
  298.     {
  299.     int i;
  300.     extern char *malloc ();
  301.     
  302.     for (i = 0; i < LIBMAX && liblist[t][i] != NULL; i++)
  303.         ;
  304.     if (i == LIBMAX)
  305.         goof ("Too many libraries for \"%s\" (max %d).\n",
  306.             targets[t], LIBMAX);
  307.     else
  308.         {
  309.         liblist[t][i] = malloc ((unsigned) strlen (file) + 1);
  310.         if (liblist[t][i] == NULL)
  311.             nomemory ();
  312.         strcpy (liblist[t][i], file);
  313.         }
  314.     }
  315.  
  316. /*
  317.  * Add a filename to the list of "other dependencies" for the given
  318.  * target. These must be copied to permanent storage.
  319.  */
  320. add_depend (t, file)
  321. int t;        /* Index of the target    */
  322. char *file;    /* File name        */
  323.     {
  324.     int i;
  325.     extern char *malloc ();
  326.     
  327.     for (i = 0; i < DEPENDMAX && deplist[t][i] != NULL; i++)
  328.         ;
  329.     if (i == DEPENDMAX)
  330.         goof ("Too many extra dependencies for \"%s\" (max %d).\n",
  331.             targets[t], DEPENDMAX);
  332.     else
  333.         {
  334.         deplist[t][i] = malloc ((unsigned) strlen (file) + 1);
  335.         if (deplist[t][i] == NULL)
  336.             nomemory ();
  337.         strcpy (deplist[t][i], file);
  338.         }
  339.     }
  340.  
  341. /*
  342.  * Add a filename to the list of resource sources for the given
  343.  * target. These must be copied to permanent storage.
  344.  */
  345. add_res (t, file)
  346. int t;        /* Index of the target    */
  347. char *file;    /* File name        */
  348.     {
  349.     int i;
  350.     extern char *malloc ();
  351.     
  352.     for (i = 0; i < RESMAX && reslist[t][i] != NULL; i++)
  353.         ;
  354.     if (i == RESMAX)
  355.         goof ("Too many resource dependencies for \"%s\" (max %d).\n",
  356.             targets[t], RESMAX);
  357.     else
  358.         {
  359.         reslist[t][i] = malloc ((unsigned) strlen (file) + 1);
  360.         if (reslist[t][i] == NULL)
  361.             nomemory ();
  362.         strcpy (reslist[t][i], file);
  363.         }
  364.     }
  365.  
  366. /*
  367.  * Look in the environment for "AIncludes", "CIncludes", "PInterfaces",
  368.  * and "RIncludes", each of which if present should be
  369.  * a comma-separated list of pathnames.  Add each pathname to the list
  370.  * of include directories.
  371.  */
  372. add_def_dirs ()
  373.     {
  374.     int i;
  375.     char c;
  376.     char *s, *t;
  377.     static char **lists[] = { adirlist, cdirlist, pdirlist, rdirlist };
  378.     static char *names[] = { "AIncludes", "CIncludes", "PInterfaces", "RIncludes" };
  379.     extern char *getenv ();
  380.     
  381.     for (i = 0; i < 4; i++)
  382.         {
  383.         if ((s = getenv (names[i])) == NULL)
  384.             continue;
  385.         
  386.         while (*s != EOS)
  387.             {
  388.             for (t = s; *t != EOS && *t != ','; t++)
  389.                 ;
  390.             c = *t;
  391.             *t = EOS;
  392.             add_dir (s, lists[i]);
  393.             *t = c;
  394.             if (*t != EOS)
  395.                 t++;
  396.             s = t;
  397.             }
  398.         }
  399.     }
  400.  
  401. nomemory ()
  402.     {
  403.     OSgoof ("Out of memory.\n");
  404.     exit (EXIT_RES);
  405.     }
  406.  
  407. goof (ms, a,b,c,d,e,f,g,h,i,j)
  408.     {
  409.     fprintf (stderr, "### %s - ", progname);
  410.     fprintf (stderr, ms, a,b,c,d,e,f,g,h,i,j);
  411.     }
  412.  
  413. OSgoof (ms, a,b,c,d,e,f,g,h,i,j)
  414.     {
  415.     char errbuf[256];
  416.     
  417.     goof (ms, a,b,c,d,e,f,g,h,i,j);
  418.     fprintf (stderr, "# %s\n", GetSysErrText (MacOSErr, errbuf));
  419.     }
  420.  
  421. usage ()
  422.     {
  423.     fprintf (stderr, "# %s", Usage);
  424.     }
  425.  
  426. progress (ms, a,b,c,d,e,f,g,h,i,j)
  427. char *ms;
  428.     {
  429.     if (verbose)
  430.         fprintf (stderr, ms, a,b,c,d,e,f,g,h,i,j);
  431.     }